﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Xml;
using ZombieMonkeys.Vibria.Logic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Text;

namespace ZombieMonkeys.Vibria.DataLayer
{
    public class PuzzleSaver
    {
        public void Save(Puzzle puzzle)
        {
            string value = string.Empty;
            foreach (Square square in puzzle.Grid.SquareList)
            {
                if (square != puzzle.Grid.SquareList[puzzle.Grid.SquareList.Count - 1])
                {
                    value += square.Value.ToString() + ",";
                }
                else
                {
                    value += square.Value.ToString();
                }
            }
            
            string path = @"Assets\SudokunatortronData.xml";

            try
            {
                XDocument doc = new XDocument();
                int i = 1;
                XElement element;
                IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
                if (!file.FileExists(path))
                {
                    doc = XDocument.Load(path);

                    foreach (XElement elem in doc.Root.Elements("Puzzle"))
                    {
                        i++;
                    }
                    element = new XElement(new XElement("Puzzle", new XAttribute("Id", i), new XElement("Grid", value)));
                    file.CreateDirectory("Assets");
                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(path, FileMode.Append, FileAccess.Write, file))
                    {
                        doc.Root.Add(element);
                        doc.Save(stream);
                        stream.Close();
                    }
                }
                else
                {
                    using (IsolatedStorageFileStream stream = file.OpenFile(path, FileMode.Open, FileAccess.ReadWrite))
                    {
                        doc = XDocument.Load(stream);
                        foreach (XElement elem in doc.Root.Elements("Puzzle"))
                        {
                            i++;
                        }
                        stream.Close();
                        element = new XElement(new XElement("Puzzle", new XAttribute("Id", i), new XElement("Grid", value)));
                        doc.Root.Add(element);
                        var newstream = new IsolatedStorageFileStream(path, FileMode.Create, FileAccess.Write, file);
                        doc.Save(newstream);
                        newstream.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was a problem saving:" + ex.Message);
            }
        }
    }
}
